import { ListResult } from '@/data/result';
import { list } from '@/services/data';
import useSWR from 'swr';

export const useAdminPermissions = () => {
  const {
    data: adminPermissions = [],
    mutate,
    isLoading,
  } = useSWR(
    '/system/rbacPermission',
    async (url: any) => {
      const data: ListResult = await list(url, { type: 1 });
      return data.list || [];
    },
    {
      revalidateOnFocus: false, // 默认不重新验证
      revalidateOnMount: true, // 组件挂载时验证
      revalidateIfStale: true, // 数据过期时重新验证
      dedupingInterval: 30000, // 30秒内重复请求返回缓存数据
      refreshInterval: 0, // 不自动刷新
    },
  );

  return {
    adminPermissions,
    isLoading,
    mutate,
  };
};

export const useOpenPermissions = () => {
  const {
    data: openPermissions = [],
    mutate,
    isLoading,
  } = useSWR(
    '/system/rbacPermission',
    async (url: any) => {
      const data: ListResult = await list(url, { type: 2 });
      return data.list || [];
    },
    {
      revalidateOnFocus: false, // 默认不重新验证
      revalidateOnMount: true, // 组件挂载时验证
      revalidateIfStale: true, // 数据过期时重新验证
      dedupingInterval: 30000, // 30秒内重复请求返回缓存数据
      refreshInterval: 0, // 不自动刷新
    },
  );

  return {
    openPermissions,
    isLoading,
    mutate,
  };
};
